package org.tis.tools.abf.module.om.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.om.controller.request.*;
import org.tis.tools.abf.module.om.entity.OmPosition;
import org.tis.tools.abf.module.om.entity.enums.OmPositionStatus;
import org.tis.tools.abf.module.om.entity.enums.OmPositionType;
import org.tis.tools.abf.module.om.service.IOmPositionService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * omPosition的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/omPosition")
@Slf4j
public class OmPositionController extends BaseController<OmPosition>  {

    @Autowired
    private IOmPositionService omPositionService;

    @ApiOperation(value = "新增根岗位",notes = "传入OmPositionRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增根岗位")
    @PostMapping("/addRoot")
    public ResultVO addRoot(@RequestBody @Validated  @ApiParam(name = "OmPositionRequest" ,value = "传入的OmPositionRequest类",required = true) OmPositionRequest omPositionRequest) {
        if (omPositionService.existAddPositionCode(omPositionRequest.getPositionCode(),omPositionRequest.getGuidMapping())){
            return ResultVO.error("404", "岗位代码已经存在,请重新输入!");
        }
        boolean isexist = omPositionService.addRoot(omPositionRequest);
        return ResultVO.success("新增成功！");
    }

    @ApiOperation(value = "新增子岗位",notes = "传入OmPositionRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增子岗位")
    @PostMapping("/addChild")
    public ResultVO addChild(@RequestBody @Validated @ApiParam(name = "OmPositionRequest" ,value = "传入的OmPositionRequest类",required = true) OmPositionRequest omPositionRequest) {
        if (omPositionService.existAddPositionCode(omPositionRequest.getPositionCode(),omPositionRequest.getGuidMapping())){
            return ResultVO.error("404", "岗位代码已经存在,请重新输入!");
        }
        omPositionRequest.setPositionType(OmPositionType.ORGANIZATION);
        boolean isexist = omPositionService.addChild(omPositionRequest);
        return ResultVO.success("新增成功！");
    }

    @ApiOperation(value = "修改机构下岗位",notes = "传入OmPositionRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改机构下岗位")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(name = "OmPositionRequest" ,value = "传入的OmPositionRequest类",required = true) OmPositionRequest omPositionRequest) {
        OmPosition omPosition = omPositionService.selectById(omPositionRequest.getGuid());
        if (omPosition == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (omPositionService.existUpdatePositionCode(omPositionRequest.getPositionCode(),omPositionRequest.getGuid(),omPositionRequest.getGuidMapping())){
            return ResultVO.error("404", "岗位代码已经存在,请重新输入!");
        }
        omPosition = omPositionService.change(omPositionRequest);

        OmPosition omPositionQuery = omPositionService.selectById(omPositionRequest.getGuid());
        return ResultVO.success("修改成功!",omPositionQuery);
    }


    @ApiOperation(value = "删除岗位",notes = "传入岗位GUID(id)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除岗位")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id" ,value = "岗位GUID",required = true) String id) {
        OmPosition omPosition = omPositionService.selectById(id);
        if (omPosition == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omPositionService.deleteRoot(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "批量删除岗位",notes = "传入OmBatchDeleteRequest类参数")
    @OperateLog(type = OperateType.DELETE,desc = "批量删除岗位")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(name = "OmBatchDeleteRequest" ,value = "传入的OmBatchDeleteRequest类",required = true) OmBatchDeleteRequest deleteRequest) {
        omPositionService.batchDelete(deleteRequest);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据ID查询岗位",notes = "传入岗位GUID作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id" ,value = "岗位GUID",required = true) String id) {
        OmPosition omPosition = omPositionService.selectById(id);
        if (omPosition == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", omPosition);
    }


    @ApiOperation(value = "分页查询所有的岗位",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(name = "page" ,value = "分页信息",required = true) SmartPage<OmPosition> page) {
        return  ResultVO.success("查询成功", omPositionService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "不分页查询所有的岗位",notes = "不需要传入参数")
    @GetMapping("/allPositionList")
    public ResultVO queryAllPosition(){
        List<OmPosition> list = omPositionService.QueryAllPosition();
        return ResultVO.success("查询成功",list);
    }


    @ApiOperation(value = "查询岗位的树",notes = "传入岗位GUID作为参数")
    @PostMapping("/tree/{guid}")
    public ResultVO tree(@PathVariable @NotBlank(message = "guid不能为空") @ApiParam(name = "guid" ,value = "岗位GUID",required = true) String guid){

        OmPosition omPosition = omPositionService.selectById(guid);
        if (omPosition == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功！",omPositionService.queryPositionTree(guid));
    }


    @ApiOperation(value = "分页查询机构下的岗位",notes = "传入机构GUID和分页信息作为参数")
    @PostMapping("/treeByOrgId/{guid}")
    public ResultVO treeByOrgId(@RequestBody @Validated @ApiParam(name = "page" ,value = "分页信息",required = true) SmartPage<OmPosition> page, @PathVariable @NotBlank(message = "组织机构guid不能为空") @ApiParam(name = "guid" ,value = "机构GUID",required = true) String guid){
        return ResultVO.success("查询成功！",omPositionService.treeByOrgId(page.getPageObj(),page.getConditionWrapper(),guid));
    }


    @ApiOperation(value = "不分页查询机构下的所属岗位",notes = "传入机构GUID(orgId)和岗位GUID(guid)作为参数")
    @PostMapping("/listsByOrgId")
    public ResultVO queryPositionByOrgId(@RequestBody @Validated @ApiParam(name = "OmPositionOrgRequest" ,value = "传入参数OmPositionOrgRequest",required = true) OmPositionOrgRequest omPositionOrgRequest){
        return ResultVO.success("查询成功",omPositionService.QueryPositionByOrgId(omPositionOrgRequest.getOrgId(),
                omPositionOrgRequest.getGuid()));
    }


    @ApiOperation(value = "设置岗位有效时间",notes = "传入OmOrgSetDateRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "设置岗位的有效时间")
    @PutMapping("/setDate")
    public ResultVO setDate(@RequestBody @Validated @ApiParam(name = "OmOrgSetDateRequest" ,value = "传入的OmOrgSetDateRequest类",required = true) OmOrgSetDateRequest omOrgSetDateRequest){
        OmPosition omPosition = omPositionService.selectById(omOrgSetDateRequest.getGuid());
        if (null == omPosition){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        //收集参数
        omPosition.setStartDate(omOrgSetDateRequest.getStartDate());
        omPosition.setEndDate(omOrgSetDateRequest.getEndDate());

        omPositionService.updateById(omPosition);

        return ResultVO.success("设置成功！",omPosition);
    }


    @ApiOperation(value = "注销岗位",notes = "传入岗位GUID作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "注销岗位")
    @PutMapping("/cancelStatus/{id}")
    public ResultVO cancelStatus(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "guid" ,value = "岗位GUID",required = true) String id){
        OmPosition omPosition = omPositionService.selectById(id);
        if (null == omPosition){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmPositionStatus.RUNNING != omPosition.getPositionStatus()){
            return ResultVO.error("404", "只有机构状态为正常时才可以注销！");
        }

        omPosition.setPositionStatus(OmPositionStatus.CANCEL);
        omPositionService.updateById(omPosition);

        return ResultVO.success("注销成功！",omPosition);
    }


    @ApiOperation(value = "启用岗位",notes = "传入岗位GUID作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "启用岗位")
    @PutMapping("/runningStatus/{id}")
    public ResultVO runningStatus(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "guid" ,value = "岗位GUID",required = true) String id){
        OmPosition omPosition = omPositionService.selectById(id);
        if (null == omPosition){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmPositionStatus.CANCEL != omPosition.getPositionStatus()){
            return ResultVO.error("404", "只有机构状态为注销时才可以启用！");
        }

        omPosition.setPositionStatus(OmPositionStatus.RUNNING);
        omPositionService.updateById(omPosition);

        return ResultVO.success("启用成功！",omPosition);
    }

    @ApiOperation(value = "新增岗位时岗位代码是否重复",notes = "传入岗位代码(positionCode)作为参数")
    @PostMapping("/existAddPositionCode")
    public ResultVO existAddPositionCode(@RequestBody @Validated @ApiParam(value = "OmPositionCodeRequest",name = "传入的OmPositionCodeRequest类", required = true) OmPositionCodeRequest omPositionCodeRequest){
        return ResultVO.success("查询成功",omPositionService.existAddPositionCode(omPositionCodeRequest.getPositionCode
                (),omPositionCodeRequest.getGuidMapping()));
    }

    @ApiOperation(value = "修改岗位时岗位代码是否重复",notes = "传入岗位代码(positionCode)和GUID(guid)作为参数")
    @PostMapping("/existUpdatePositionCode")
    public ResultVO existUpdatePositionCode(@RequestBody @Validated @ApiParam(value = "OmPositionCodeRequest",name = "传入的OmPositionCodeRequest类", required = true) OmPositionCodeRequest omPositionCodeRequest){
        OmPosition omPosition = omPositionService.selectById(omPositionCodeRequest.getGuid());
        if (omPosition == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功",omPositionService.existUpdatePositionCode(omPositionCodeRequest.getPositionCode(),
                omPositionCodeRequest.getGuid(),omPositionCodeRequest.getGuidMapping()));
    }

    /** 下面是工作组的接口 */

    @ApiOperation(value = "删除工作组岗位",notes = "传入岗位GUID(id)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除工作组岗位")
    @DeleteMapping("/{id}/group")
    public ResultVO deleteGroup(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id" ,value = "岗位GUID",
            required = true) String id) {
        OmPosition omPosition = omPositionService.selectById(id);
        if (omPosition == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omPositionService.deleteRoot(id);
        return ResultVO.success("删除成功");
    }

    @ApiOperation(value = "批量删除工作组岗位",notes = "传入OmBatchDeleteRequest类参数")
    @OperateLog(type = OperateType.DELETE,desc = "批量删除工作组岗位")
    @PostMapping("/batchDeleteInGroup")
    public ResultVO batchDeleteInGroup(@RequestBody @Validated @ApiParam(name = "OmBatchDeleteRequest" ,value = "传入的OmBatchDeleteRequest类",required = true) OmBatchDeleteRequest deleteRequest) {
        omPositionService.batchDelete(deleteRequest);
        return ResultVO.success("删除成功");
    }

    @ApiOperation(value = "注销工作组岗位",notes = "传入岗位GUID作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "注销工作组岗位")
    @PutMapping("/{id}/cancelStatusInGroup")
    public ResultVO cancelStatusInGroup(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "guid" ,value = "岗位GUID",required = true) String id){
        OmPosition omPosition = omPositionService.selectById(id);
        if (null == omPosition){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmPositionStatus.RUNNING != omPosition.getPositionStatus()){
            return ResultVO.error("404", "只有机构状态为正常时才可以注销！");
        }

        omPosition.setPositionStatus(OmPositionStatus.CANCEL);
        omPositionService.updateById(omPosition);

        return ResultVO.success("注销成功",omPosition);
    }


    @ApiOperation(value = "启用工作组岗位",notes = "传入岗位GUID作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "启用工作组岗位")
    @PutMapping("/{id}/runningStatusInGroup")
    public ResultVO runningStatusInGroup(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "guid" ,value = "岗位GUID",required = true) String id){
        OmPosition omPosition = omPositionService.selectById(id);
        if (null == omPosition){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmPositionStatus.CANCEL != omPosition.getPositionStatus()){
            return ResultVO.error("404", "只有机构状态为注销时才可以启用！");
        }

        omPosition.setPositionStatus(OmPositionStatus.RUNNING);
        omPositionService.updateById(omPosition);

        return ResultVO.success("启用成功",omPosition);
    }

    @ApiOperation(value = "修改工作组下岗位",notes = "传入OmPositionRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改工作组下岗位")
    @PutMapping("/changeInGroup")
    public ResultVO changeInGroup(@RequestBody @Validated @ApiParam(name = "OmPositionRequest" ,value = "传入的OmPositionRequest类",required = true) OmPositionRequest omPositionRequest) {
        OmPosition omPosition = omPositionService.selectById(omPositionRequest.getGuid());
        if (omPosition == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (omPositionService.existUpdatePositionCode(omPositionRequest.getPositionCode(),omPositionRequest.getGuid(),omPositionRequest.getGuidMapping())){
            return ResultVO.error("404", "岗位代码已经存在,请重新输入!");
        }
        omPositionService.change(omPositionRequest);

        OmPosition omPositionQuery = omPositionService.selectById(omPositionRequest.getGuid());
        return ResultVO.success("修改成功",omPositionQuery);
    }

}

